package dao.server;

import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class TestModule {
	private int id;
	private int idProject;
	private String name;
	private String time;
	private String description;
	private ArrayList<TestCase> listTestCase;
	
	public TestModule(){
		name = "";
		time = "";
		description = "";
		listTestCase = new ArrayList<TestCase>();
	}
	
	public TestModule(int idProject, String name, String time,
			String description) {
		this.idProject = idProject;
		this.name = name;
		this.time = time;
		this.description = description;
		this.listTestCase = new ArrayList<TestCase>();
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getIdProject() {
		return idProject;
	}
	public void setIdProject(int idProject) {
		this.idProject = idProject;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	public ArrayList<TestCase> getListTestCase() {
		return listTestCase;
	}

	public void setListTestCase(ArrayList<TestCase> listTestCase) {
		this.listTestCase = listTestCase;
	}
	
	public void getModule(DAO dao){
		listTestCase = dao.getAllTestCaseInTestModule(id);
	}
	
	public StringWriter objectToXml(DocumentBuilder docBuilder) {
		try {
			// document elements
			Document doc = docBuilder.newDocument();
			Element document = doc.createElement("document");
			doc.appendChild(document);

			// set attribute to document element
			Attr type = doc.createAttribute("type");
			type.setValue("TESTMODULE");
			document.setAttributeNode(type);
			
			Attr action = doc.createAttribute("action");
			action.setValue("");
			document.setAttributeNode(action);

			// head elements
			Element head = doc.createElement("head");
			document.appendChild(head);

			// id elements
			Element id = doc.createElement("id");
			id.appendChild(doc.createTextNode(String.valueOf(this.getId())));
			head.appendChild(id);
			
			// name elements
			Element name = doc.createElement("name");
			name.appendChild(doc.createTextNode(this.getName()));
			head.appendChild(name);

			// time elements
			Element time = doc.createElement("time");
			time.appendChild(doc.createTextNode(this.getTime()));
			head.appendChild(time);
			
			// description elements
			Element description = doc.createElement("description");
			time.appendChild(doc.createTextNode(this.getDescription()));
			head.appendChild(description);
			
			// body elements
			Element body = doc.createElement("body");			
			document.appendChild(body);
			
			// module elements
			Element module = doc.createElement("module");
			body.appendChild(module);

			if (listTestCase != null && listTestCase.size() != 0) {
				for (TestCase tc : listTestCase) {
					// test case elements
					Element testcase = doc.createElement("testcase");
					testcase.appendChild(doc.createTextNode(tc.getName()));
					module.appendChild(testcase);

					// set attribute to test case element
					Attr idTC = doc.createAttribute("id");
					idTC.setValue(String.valueOf(tc.getId()));
					testcase.setAttributeNode(idTC);
				}
			}

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			transformer.transform(source, result);
			return sw;
		} catch (Exception e) {
			//e.printStackTrace();
			return null;
		}
	}
	public void xmlToObject(Document doc){
		try {
			NodeList nodeList = doc.getElementsByTagName("head");
			Element element = (Element)nodeList.item(0);
			try{
				this.setId(Integer.parseInt(element.getElementsByTagName("id").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setId(0);
			}
			try{
				this.setIdProject(Integer.parseInt(element.getElementsByTagName("id_project").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setId(0);
			}
			this.setName(element.getElementsByTagName("name").item(0).getTextContent());
			this.setTime(element.getElementsByTagName("time").item(0).getTextContent());
			this.setDescription(element.getElementsByTagName("description").item(0).getTextContent());
			
			NodeList moduleList = doc.getElementsByTagName("module");
            Element module = (Element)moduleList.item(0);

            NodeList testcaseList = module.getElementsByTagName("testcase");

            for (int i = 0; i < testcaseList.getLength(); i++)
            {
                TestCase tc = new TestCase();
                Element testcase = (Element)testcaseList.item(i);

                tc.setIdTestModule(id);
                try{
    				tc.setId(Integer.parseInt(element.getElementsByTagName("id").item(0).getTextContent()));
    			}catch(NumberFormatException nfe){
    				this.setId(0);
    			}
                tc.setName(testcase.getTextContent());
                listTestCase.add(tc);
            }
		} catch (Exception e) {
			//e.printStackTrace();
		}
	}
	
	public String addTestModule(DAO dao){
		if(dao != null)
			if(dao.addTestModule(this))
				return String.valueOf(id);
			else
				return "Create test module " + this.getName() + " unsuccess, please try again!";
		return "Create test module " + this.getName() + " unsuccess, please try again!";
	}
	public String updateTestModule(DAO dao){
		if(dao != null){
			TestModule tm = dao.getTestModule(this.getId());
			if(tm == null || tm.getId() == 0)
				return "Can't find the test module";
			if(dao.updateTestModule(this))
				return "Update test module success";					
		}
		return "Update test module unsuccess, please try again!";
	}
	public String renameTestModule(DAO dao){
		if(dao != null){
			TestModule tm = dao.getTestModule(this.getId());
			if(tm == null || tm.getId() == 0)
				return "Can't find the test module";
			if(dao.renameTestModule(this))
				return "Rename test module success";
		}
		return "Rename test module unsuccess, please try again!";
	}
	public String deleteTestModule(DAO dao){
		if(dao != null){
			TestModule tm = dao.getTestModule(this.getId());
			if(tm == null || tm.getId() == 0)
				return "Can't find the test module";
			if(dao.deleteTestModule(this))
				return "Remove test module " + this.getName() + " success";
		}
		return "Remove test module " + this.getName() + " unsuccess, please try again!";
	}
}
